import api from '@/api';
import * as types from '../mutation-types';

// initial state
const state = {
    authority: []
};

const mutations = {
    [types.GET_USER_AUTHORITY](state, authority) {
        state.authority = authority;
    }
};

const getters = {
    checkUserAuthority(state) {
        return (path) => {
            let hasAuthor = false;
            state.authority.forEach((item) => {
                if (item.path === path) {
                    hasAuthor = true;
                }
            });
            return hasAuthor;
        };
    }
};

const actions = {
    getUserAuthority({ commit, state }) {
        api.systemManage.getResourceList().then((res) => {
            // console.log(res);
            const { status, data, error } = res;
            if ('1' === status && '00000000' === error) {
                commit(types.GET_USER_AUTHORITY, data.resourceList);
            }
        });
    }
};

export default {
    state,
    mutations,
    actions,
    getters
};
